﻿using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Demo.Api.Services;
using KmFlow;
using Microsoft.AspNetCore.Mvc;

namespace Demo.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class TestDataController : ControllerBase
    {
        private readonly NodeRepository _nodeRepository;
        private readonly LineRepository _lineRepository;

        public TestDataController(NodeRepository nodeRepository, LineRepository lineRepository)
        {
            _nodeRepository = nodeRepository;
            _lineRepository = lineRepository;
        }

        [HttpGet]
        public async Task<IActionResult> GetTxt(long id)
        {
            var idC = new Dictionary<long, int>();
            var nodes = await _nodeRepository.GetsAsync(id);
            var lines = await _lineRepository.GetsAsync(id);
            var index = 1;
            var txt = new StringBuilder();
            foreach (var node in nodes)
            {
                idC.Add(node.Id, index);
                txt.AppendLine($"new Node {{ Id = {index},Logic =(LogicType){node.Logic}, Name = \"{node.Name}\", OrClear = {node.OrClear.ToString().ToLowerInvariant()}, NodeType = (NodeType){node.NodeType}, SeparateNodeId = {node.SeparateNodeId},RuleId = {node.RuleId} }},");
                index++;
            }
            txt.AppendLine();
            txt.AppendLine();
            txt.AppendLine();
            index = 1;
            foreach (var line in lines)
            {
                var tailId = idC[line.TailNodeId];
                var headId = idC[line.HeadNodeId];
                txt.AppendLine($"new Line {{Id = {index++}, TailNodeId = {tailId},HeadNodeId = {headId}, LineType = (LineType){line.LineType}, Code = \"{line.Code}\"}},");
            }
            return Ok(txt.ToString());
        }
    }
}